package com.jelly.web.modules.system.quartz.controller;

import com.jelly.common.constant.Constant;
import com.jelly.common.constant.GlobalConstant;
import com.jelly.common.note.OperationLog;
import com.jelly.common.util.StringUtils;
import com.jelly.common.util.web.AjaxPostResponse;
import com.jelly.common.web.page.LayuiTableInfo;
import com.jelly.config.quartz.MySchedulerFactory;
import com.jelly.web.share.base.controller.BaseController;
import com.jelly.web.share.base.domain.Base;
import com.jelly.web.modules.system.quartz.domain.Quartz;
import com.jelly.web.modules.system.quartz.service.IQuartzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @创建人 ql
 * @创建时间 2018-09-26 14:47
 * @项目名称 jelly
 * @功能描述: 定时任务控制器
 **/
@Controller
@RequestMapping("/admin/monitor/quartz")
public class QuartzController extends BaseController {

    @Autowired
    private IQuartzService quartzService;

    @Autowired
    private MySchedulerFactory mySchedulerFactory;

    /**
     * 定时任务列表跳转
     * @return
     */
    @GetMapping
    public String getQuartzList(){

        //获取字典详情
        getDictionary();

        //获取该用户可访问的按钮详情
        getButton();

        return adminQuartz("/list");

    }
    /**
     * 获取定时任务列表
     * @return
     */
    @PostMapping
    @ResponseBody
    public LayuiTableInfo getQuartzList(Quartz quartz){

        getPageHelper();

        return getPageHelper(quartzService.getQuartzAll(quartz));

    }

    /**
     * 新增定时任务跳转
     * @return
     */
    @GetMapping("/add")
    public String addQuartz(){
        //获取字典详情
        getDictionary();

        return adminQuartz("/add");

    }
    /**
     * 新增定时任务
     * @param quartz
     * @return
     */
    @PostMapping("/add")
    @OperationLog(name = "定时任务模块",value = GlobalConstant.ADDED)
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> addUser(Quartz quartz){

        if(quartzService.addQuartz(quartz)){
            try {

                mySchedulerFactory.scheduleJobs();

                return okResponse("新增成功");

            }catch (Exception e){

            }

        }

        return errorResponse("新增失败");

    }

    /**
     * 修改定时任务跳转
     * @return
     */
    @GetMapping("/update")
    public String getQuartzEdit(Model model, Base base){

        //获取字典详情
        getDictionary();

        model.addAttribute("quartz",quartzService.getQuartz(base.getBaseId()));

        return adminQuartz("/update");

    }
    /**
     * 修改定时任务
     * @param quartz
     * @return
     */
    @PostMapping("/update")
    @OperationLog(name = "定时任务模块",value = GlobalConstant.UPDATE)
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> updateQuartz(Quartz quartz){

        boolean bol = false;

        if(null !=quartz){

            if(StringUtils.isNotBlank(quartz.getIsState())){

                try {

                    if(quartz.getIsState().equals(Constant.SUCCESS)){

                        bol = mySchedulerFactory.resumeJob(quartz);

                    }else{

                        bol = mySchedulerFactory.pauseJob(quartz);

                    }

                }catch (Exception e){

                }

            }

        }

        return bol?okResponse("修改成功"):errorResponse("修改失败");

    }


    /**
     * 删除定时任务
     * @param base
     * @return
     */
    @PostMapping("/delect")
    @OperationLog(name = "定时任务模块",value = GlobalConstant.DELECT)
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> delectUser(Base base){

        if(StringUtils.isNotBlank(base.getBaseId())){

            if(quartzService.delectQuartz(base.getBaseId())){

                try {

                    mySchedulerFactory.scheduleJobs();

                    return okResponse("删除成功");

                }catch (Exception e){

                }

            }

        }

        return errorResponse("删除失败");

    }

}